﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cit.CDE.Net.Module.Commons;
using Module;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using UI.UserControl;
using Cit.CDE.Net.Service.ModuleFactory;
using System.Web.UI.HtmlControls;

/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        SplistView.aspx.cs           
/// Author :         Wangbc
/// CDT:             2013-02-01           
/// Version:         1.0.0.0                
/// Depiction:       销售政策查询   
/// </summary>
public partial class main_sales_SpListView : UserControl.CommonPage
{
    /// <summary>
    /// EntityManager
    /// </summary>
    private CDEEntityManager em;
    private String EM;
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.strHc = "1";
        //设置母版页控件显示
        ((Literal)Master.FindControl("lblpath")).Text = MenuPath;

        EM = Request.QueryString[WEBConstants.EM_QUERY_STRING];

        //初始化EM
        if (!Cit.CDE.Net.Module.Commons.CDEConstants.IsEmpty(EM))
        {
            em = cde.cache.CDEFactory.getEMByFullName(EM);
        }
        else
        {
            em = null;
        }

        Master.sc = this.CreatSc(em);

        //设置母版页控件事件
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ItemDataBound += new DataGridItemEventHandler(this.itemDataBound);


    }
    /// <summary>
    /// 操作事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void itemDataBound(object sender, DataGridItemEventArgs e)
    {
        //控制母版页控件显示内容
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns[((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns.Count - 1].Visible = true;
    }

    /// <summary>
    /// 删除操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdDel_Click(object sender, EventArgs e)
    {

        string strErr = string.Empty;
        string strMp = string.Empty;
        
        List<CDEEntity> S_listEntity = (List<CDEEntity>)Session[WEBConstants.LIST_SESSION_ID + EM];
        
        List<CDEEntity> listEntity = new List<CDEEntity>();
        foreach (int index in Master.listIndex)
        {
            S_listEntity[index].Operation = CDEDataOperation.DELETE;       
            listEntity.Add(S_listEntity[index]);
        }

        //更新
        CDEService.UpdateEntities(em, listEntity);
        //移除表格中的数据
        foreach (int index in Master.listIndex)
        {
            S_listEntity.Remove(S_listEntity[index]);
        }

        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ListEntity = S_listEntity;

        Session[WEBConstants.LIST_SESSION_ID + EM] = S_listEntity;

        Master.GetFirstNum();

        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).DataBind();

        for (int index = 0; index < listEntity.Count; index++)
        {
            if (listEntity[index].Result == CDEDataOperationResult.ERROR)
            {
                strErr = strErr + (((main_main_MasterPage)this.Master).listIndex[index] + 1).ToString() + ",";
            }
        }
 

        if (strErr != string.Empty)
        {
            strErr = strErr.Substring(0, strErr.Length - 1);
            strMp = Resources.ResourceErrMsg.DELETE_ERR;
            strMp = strMp.Replace("{0}", strErr);
            showMessageErrAndOk(strMp);
        }
        else
        {
            showMessageErrAndOk(Resources.ResourceAdmin.MSG_ROLE_DEL_RESULT_OK);
        }
        
        //重置页面条数
        Master.refreshPage();
        Master.refreshResult();
        //Master.dispSortImg();
        
    }
}
